
<template>
    <div class='input-number'>
         <el-input-number v-model="currentValue" :min="mins" :max="maxs" ></el-input-number>
        {{last_span}}
    </div>
</template>
 
<script>
    export default {
        props:['max','min','value','extype','exkey','id','data','exkey3','dataField','index','exdata'],
        name: "input-number",
        data(){
            return {
                currentValue:this.value,
                pId:"",
                cId:"",
                mins:1,
                maxs:1000,
                last_span:"",
                amtStr:""
            }
        },
        created(){
            try{
                this.last_span=this.exkey3.span_last;
            }catch(e){}
            try{
                if(this.exkey.amtStr){
                    this.amtStr=this.exkey.amtStr;
                }else{
                    this.amtStr=this.exkey;
                }
            }catch(e){}
            this.setData();
            let sef=this;
            window.setTimeout(function(){
                sef.changeData(sef.currentValue);
            },1500)
            try{
                if(this.exkey.min&&this.exkey.min!=undefined){
                    this.mins=Number(this.exkey.min);
                }
            }catch(e){}
             try{
                if(this.exkey.max&&this.exkey.max!=undefined){
                    this.maxs=Number(this.exkey.max);
                }
            }catch(e){}
            console.log(this.id+"=="+this.mins+"="+this.maxs,"this.min==========================")
            
        },
         computed:{
            dataFields:function(){
                let ids=this.id.split(".");
                if(ids.length>1){
                    this.pId=ids[0];
                    this.cId=ids[1]
                }
                if(this.pId){
                   
                    return this.data[this.pId][this.cId];
                }
               
            }
        },
        watch:{
            currentValue: function (val) { //currentValue值变动就向父组件传值
                console.log("this.val==================="+val+"=="+this.pId+"=="+this.cId+"=="+this.dataField+"=="+this.index+"="+this.id+"="+this.exdata);
                if(this.dataField){
                    if(this.exdata||this.exdata=='0'){
                        this.$set(this.data[this.dataField][this.exdata],this.id,val);
                    }else
                    this.$set(this.data[this.dataField][this.index],this.id,val);
                }else
                    this.$set(this.data[this.pId],this.cId,val);
                
                console.log(this.data,"this.data==========")
                this.changeData(val);

                if(this.exkey&&this.exkey.xm){
                    this.exkey.xm.forEach(item => {
                        let datas={}
                        datas.addType="string";
                        datas.val=item.name;
                        datas.nameType="";
                        datas.hour="";
                        datas.isAdd="Y";
                        datas.day=val;
                    
                        this.$emit("funeralItemSelectEvent",JSON.stringify(datas))
                    });
                    
                }
            },
            dataFields:{
                handler: function (newVal) {
                   
                  //if(this.currentValue!=this.data[this.pId][this.cId])
                
                       // this.setData()
                  this.currentValue=newVal;
                },      
                deep: true    //深度监听
            }
        },
        mounted(){
        },
        methods:{
            changeData(val){
               
                if(this.extype=="date_year"){
                    sessionStorage.setItem("管理费单位","年")
                    let startDate=this.data[this.pId][this.exkey.startDate];
                    console.log(startDate,"startDate===============================================")
                    if(startDate){
                        let date=new Date(startDate);
                        let year__=Number(val);
                        let month__=0;
                        date.setFullYear(date.getFullYear()+Number(val)); 
                        let month_val="";

                        if(this.exkey.month){
                            month_val=this.data[this.pId][this.exkey.month];
                            month__=Number(month_val);
                            date.setMonth(date.getMonth()+Number(month_val)); 
                        }
                        let monthAmt="";
                        try{
                            monthAmt=this.exkey.monthAmt;
                        }catch(e){}
                        if(monthAmt){
                            month__=year__*12+month__;
                            let priceField=Number(this.data[this.pId][this.exkey.priceField]);
                            let amt__=month__*priceField;
                           
                            this.$set(this.data[this.pId],monthAmt,amt__)

                        }
                        month_val=month_val==""? "0":month_val;
                        if(Number(val)!=0||Number(month_val)!=0){
                            if(this.exkey.nosubday!='Y')
                                date=date.setDate(date.getDate()-1);
                            date=new Date(date);
                        }
                        
                        var year = date.getFullYear();//当前时间的年份减去一年
                        var month = date.getMonth()+1;//时间的月份获取
                        var day = date.getDate();//时间的日，获取
                      
                        if(month<10){
                            month="0"+month;
                        }
                        if(day<10){
                            day="0"+day;
                        }
                        var time = year+'-'+month+'-'+day;//时间进行拼接
                        console.log("this.data[this.pid===="+this.exkey.endDate+"="+time+"=="+this.pId)
                        this.$set(this.data[this.pId],this.exkey.endDate,time);
                    }
                }
                if(this.extype=="date_month"){
                    sessionStorage.setItem("管理费单位","月")
                    let startDate=this.data[this.pId][this.exkey.startDate];
                    if(startDate){
                        let date=new Date(startDate);
                        let year__=0;
                        let month__=Number(val);
                        date.setMonth(date.getMonth()+Number(val)); 
                        let month_val="";
                        if(this.exkey.year){
                            month_val=this.data[this.pId][this.exkey.year];
                            year__=Number(month_val)
                            date.setFullYear(date.getFullYear()+Number(month_val)); 
                        }
                        month_val=month_val==""? "0":month_val;
                        if(Number(val)!=0||Number(month_val)!=0){
                            if(this.exkey.nosubday!='Y')
                                date=date.setDate(date.getDate()-1);
                            date=new Date(date);
                        }
                        let monthAmt="";
                        try{
                            monthAmt=this.exkey.monthAmt;
                        }catch(e){}
                        if(monthAmt){
                            month__=year__*12+month__;
                            let priceField=Number(this.data[this.pId][this.exkey.priceField]);
                            let amt__=month__*priceField;
                            this.$set(this.data[this.pId],monthAmt,amt__)

                        }
                        var year = date.getFullYear();//当前时间的年份减去一年
                        var month = date.getMonth()+1;//时间的月份获取
                        var day = date.getDate();//时间的日，获取
                      
                        if(month<10){
                            month="0"+month;
                        }
                        if(day<10){
                            day="0"+day;
                        }
                        var time = year+'-'+month+'-'+day;//时间进行拼接
                        console.log("this.data[this.pid===22="+this.exkey.endDate+"="+time)
                        this.$set(this.data[this.pId],this.exkey.endDate,time);
                    }
                }

                if(this.exkey.amt){
                    let num=Number(this.data[this.pId][this.exkey.num]);
                    this.$set(this.data[this.pId],this.exkey.amt,num*Number(val))
                }

                if(this.extype=="total_amt"){
                    let exkeys=this.amtStr.split(",");
                    let id_=exkeys[exkeys.length-1];
                    let total_=0;
                    for(let i=0;i<exkeys.length-1;i++){
                        let val_=Number(this.data[this.pId][exkeys[i]]);
                        total_=total_+val_;
                    }
                    this.$set(this.data[this.pId],id_,total_);
                }
            },
            setData(){
                console.log("id================"+this.id)
                
                if(!this.value){
                    if(this.data){
                        let ids=this.id.split(".");
                        if(ids.length>1){
                            let p=ids[0];
                            let id_=ids[1];
                            this.pId=p;
                            this.cId=id_;
                            console.log(this.pId+"="+this.cId)
                            if(this.index||this.index=='0'){
                                try{
                                    this.currentValue=unescape(this.data[p][this.index][id_]);
                                }catch(e){}
                            }else{
                                try{
                                    this.currentValue=unescape(this.data[p][id_]);
                                }catch(e){}
                            }
                            
                        }else{
                            if(this.dataField){
                                if(this.exdata||this.exdata=='0'){
                                    this.currentValue=this.data[this.dataField][this.exdata][this.id]
                                }
                            }else if(this.data[this.id])
                                this.currentValue= unescape(this.data[this.id]);
                        }
                        if(!this.currentValue){
                            this.currentValue="0"
                        }
                    }
                }else{
                    let data={};
                    data.id=this.id;
                    data.val=this.value;
                    data.index=this.index;
                    this.$emit('input',JSON.stringify(data));
                }
               
                
                if(this.currentValue==undefined||this.currentValue=="undefined"){
                    this.currentValue="";
                }
                
                
            },
            
        }
    }
</script>
 
<style scoped>
    .input-number{
        width:100%;
        display:flex;
        flex-direction: row;
        overflow: hidden;
    }
    input{
        flex:1;
    }
    button{
        border: none;
        background: #4e83e4;
        color: #fff;
        height: 36px;
        width: 36px;
    }
</style>